home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / EDITORS / MEMACS / C / Buffer < prev    next >
Text File  |  1990-06-26  |  14KB  |  578 lines

  1. /*  BUFFER.C:    buffer mgmt. routines
  2.         MicroEMACS 3.10
  3.  
  4.  * Buffer management.
  5.  * Some of the functions are internal,
  6.  * and some are actually attached to user
  7.  * keys. Like everyone else, they set hints
  8.  * for the display system.
  9.  */
  10. #include    <stdio.h>
  11. #include    "estruct.h"
  12. #include    "eproto.h"
  13. #include    "edef.h"
  14. #include    "elang.h"
  15.  
  16. /*
  17.  * Attach a buffer to a window. The
  18.  * values of dot and mark come from the buffer
  19.  * if the use count is 0. Otherwise, they come
  20.  * from some other window.
  21.  */
  22. PASCAL NEAR usebuffer(f, n)
  23.  
  24. int f,n;    /* prefix flag and argument */
  25.  
  26. {
  27.     register BUFFER *bp;    /* temporary buffer pointer */
  28.  
  29.     /* get the buffer name to switch to */
  30.     bp = getdefb();
  31.     bp = getcbuf(TEXT24, bp ? bp->b_bname : "main", TRUE);
  32. /*                "Use buffer" */
  33.     if (!bp)
  34.         return(ABORT);
  35.  
  36.     /* make it invisable if there is an argument */
  37.     if (f == TRUE)
  38.         bp->b_flag |= BFINVS;
  39.  
  40.     /* switch to it in any case */
  41.     return(swbuffer(bp));
  42. }
  43.  
  44. PASCAL NEAR nextbuffer(f, n)    /* switch to the next buffer in the buffer list */
  45.  
  46. int f, n;    /* default flag, numeric argument */
  47. {
  48.     register BUFFER *bp;    /* current eligable buffer */
  49.     register int status;
  50.  
  51.     /* make sure the arg is legit */
  52.     if (f == FALSE)
  53.         n = 1;
  54.     if (n < 1)
  55.         return(FALSE);
  56.  
  57.     /* cycle thru buffers until n runs out */
  58.     while (n-- > 0) {
  59.         bp = getdefb();
  60.         if (bp == NULL)
  61.             return(FALSE);
  62.         status = swbuffer(bp);
  63.         if (status != TRUE)
  64.             return(status);
  65.     }
  66.     return(status);
  67. }
  68.  
  69. PASCAL NEAR swbuffer(bp)    /* make buffer BP current */
  70.  
  71. BUFFER *bp;
  72.  
  73. {
  74.     register WINDOW *wp;
  75.     SCREEN *scrp;        /* screen to fix pointers in */
  76.     register int cmark;        /* current mark */
  77.  
  78.     /* let a user macro get hold of things...if he wants */
  79.     execkey(&exbhook, FALSE, 1);
  80.  
  81.     if (--curbp->b_nwnd == 0) {        /* Last use.        */
  82.         curbp->b_dotp  = curwp->w_dotp;
  83.         curbp->b_doto  = curwp->w_doto;
  84.         for (cmark = 0; cmark < NMARKS; cmark++) {
  85.             curbp->b_markp[cmark] = curwp->w_markp[cmark];
  86.             curbp->b_marko[cmark] = curwp->w_marko[cmark];
  87.         }
  88.         curbp->b_fcol  = curwp->w_fcol;
  89.     }
  90.     curbp = bp;                /* Switch.        */
  91.     if (curbp->b_active != TRUE) {        /* buffer not active yet*/
  92.         /* read it in and activate it */
  93.         readin(curbp->b_fname, ((curbp->b_mode&MDVIEW) == 0));
  94.         curbp->b_dotp = lforw(curbp->b_linep);
  95.         curbp->b_doto = 0;
  96.         curbp->b_active = TRUE;
  97.     }
  98.     curwp->w_bufp  = bp;
  99.     curwp->w_linep = bp->b_linep;        /* For macros, ignored. */
  100.     curwp->w_flag |= WFMODE|WFFORCE|WFHARD; /* Quite nasty.     */
  101.     if (bp->b_nwnd++ == 0) {        /* First use.        */
  102.         curwp->w_dotp  = bp->b_dotp;
  103.         curwp->w_doto  = bp->b_doto;
  104.         for (cmark = 0; cmark < NMARKS; cmark++) {
  105.             curwp->w_markp[cmark] = bp->b_markp[cmark];
  106.             curwp->w_marko[cmark] = bp->b_marko[cmark];
  107.         }
  108.         curwp->w_fcol  = bp->b_fcol;
  109.     } else {
  110.         /* in all screens.... */
  111.         scrp = first_screen;
  112.         while (scrp) {
  113.             wp = scrp->s_first_window;
  114.             while (wp != NULL) {
  115.                 if (wp!=curwp && wp->w_bufp==bp) {
  116.                     curwp->w_dotp  = wp->w_dotp;
  117.                     curwp->w_doto  = wp->w_doto;
  118.                     for (cmark = 0; cmark < NMARKS; cmark++) {
  119.                         curwp->w_markp[cmark] = wp->w_markp[cmark];
  120.                         curwp->w_marko[cmark] = wp->w_marko[cmark];
  121.                     }
  122.                     curwp->w_fcol  = wp->w_fcol;
  123.                     break;
  124.                 }
  125.                 /* next window */
  126.                 wp = wp->w_wndp;
  127.             }
  128.  
  129.             /* next screen! */
  130.             scrp = scrp->s_next_screen;
  131.         }
  132.     }
  133.  
  134.     /* let a user macro get hold of things...if he wants */
  135.     execkey(&bufhook, FALSE, 1);
  136.  
  137.     return(TRUE);
  138. }
  139.  
  140. /*
  141.  * Dispose of a buffer, by name.
  142.  * Ask for the name. Look it up (don't get too
  143.  * upset if it isn't there at all!). Get quite upset
  144.  * if the buffer is being displayed. Clear the buffer (ask
  145.  * if the buffer has been changed). Then free the header
  146.  * line and the buffer header. Bound to "C-X K".
  147.  */
  148. PASCAL NEAR killbuffer(f, n)
  149.  
  150. int f,n;    /* prefix flag and argument */
  151.  
  152. {
  153.     register BUFFER *bp;    /* ptr to buffer to dump */
  154.  
  155.     /* get the buffer name to kill */
  156.     bp = getdefb();
  157.     bp = getcbuf(TEXT26, bp ? bp->b_bname : "main", TRUE);
  158. /*             "Kill buffer" */
  159.     if (bp == NULL)
  160.         return(ABORT);
  161.  
  162.     return(zotbuf(bp));
  163. }
  164.  
  165. /*    Allow the user to pop up a buffer, like we do.... */
  166.  
  167. PASCAL NEAR popbuffer(f, n)
  168.  
  169. int f, n;    /* default and numeric arguments */
  170.  
  171. {
  172.     register BUFFER *bp;    /* ptr to buffer to dump */
  173.  
  174.     /* get the buffer name to pop */
  175.     bp = getdefb();
  176.     bp = getcbuf(TEXT27, bp ? bp->b_bname : "main", TRUE);
  177. /*             "Pop buffer" */
  178.     if (bp == NULL)
  179.         return(ABORT);
  180.  
  181.     return(pop(bp));
  182. }
  183.  
  184. BUFFER *PASCAL NEAR getdefb()    /* get the default buffer for a use or kill */
  185.  
  186. {
  187.     BUFFER *bp;    /* default buffer */
  188.  
  189.     /* Find the next buffer, which will be the default */
  190.     bp = curbp->b_bufp;
  191.  
  192.     /* cycle through the buffers to find an eligable one */
  193.     while (bp == NULL || bp->b_flag & BFINVS) {
  194.         if (bp == NULL)
  195.             bp = bheadp;
  196.         else
  197.             bp = bp->b_bufp;
  198.  
  199.         /* don't get caught in an infinite loop! */
  200.         if (bp == curbp) {
  201.             bp = NULL;
  202.             break;
  203.         }
  204.     }            
  205.     return(bp);
  206. }
  207.  
  208. PASCAL NEAR zotbuf(bp)    /* kill the buffer pointed to by bp */
  209.  
  210. register BUFFER *bp;
  211.  
  212. {
  213.     register BUFFER *bp1;
  214.     register BUFFER *bp2;
  215.     register int    s;
  216.  
  217.     if (bp->b_nwnd != 0) {            /* Error if on screen.    */
  218.         mlwrite(TEXT28);
  219. /*            "Buffer is being displayed" */
  220.         return(FALSE);
  221.     }
  222.     if ((s=bclear(bp)) != TRUE)        /* Blow text away.    */
  223.         return(s);
  224.     free((char *) bp->b_linep);        /* Release header line. */
  225.     bp1 = NULL;                /* Find the header.    */
  226.     bp2 = bheadp;
  227.     while (bp2 != bp) {
  228.         bp1 = bp2;
  229.         bp2 = bp2->b_bufp;
  230.     }
  231.     bp2 = bp2->b_bufp;            /* Next one in chain.    */
  232.     if (bp1 == NULL)            /* Unlink it.        */
  233.         bheadp = bp2;
  234.     else
  235.         bp1->b_bufp = bp2;
  236.     free((char *) bp);            /* Release buffer block */
  237.     return(TRUE);
  238. }
  239.  
  240. PASCAL NEAR namebuffer(f,n)    /*    Rename the current buffer    */
  241.  
  242. int f, n;        /* default Flag & Numeric arg */
  243.  
  244. {
  245.     register BUFFER *bp;    /* pointer to scan through all buffers */
  246.     char bufn[NBUFN];    /* buffer to hold buffer name */
  247.  
  248.     /* prompt for and get the new buffer name */
  249. ask:    if (mlreply(TEXT29, bufn, NBUFN) != TRUE)
  250. /*            "Change buffer name to: " */
  251.         return(FALSE);
  252.  
  253.     /* and check for duplicates */
  254.     bp = bheadp;
  255.     while (bp != NULL) {
  256.         if (bp != curbp) {
  257.             /* if the names the same */
  258.             if (strcmp(bufn, bp->b_bname) == 0)
  259.                 goto ask;  /* try again */
  260.         }
  261.         bp = bp->b_bufp;    /* onward */
  262.     }
  263.  
  264.     strcpy(curbp->b_bname, bufn);    /* copy buffer name to structure */
  265.     curwp->w_flag |= WFMODE;    /* make mode line replot */
  266.     mlerase();
  267.     return(TRUE);
  268. }
  269.  
  270. /*    Build and popup a buffer containing the list of all buffers.
  271.     Bound to "C-X C-B". A numeric argument forces it to list
  272.     invisable buffers as well.
  273. */
  274.  
  275. PASCAL NEAR listbuffers(f, n)
  276.  
  277. int f,n;    /* prefix flag and argument */
  278.  
  279. {
  280.     register int status;    /* stutus return */
  281.  
  282.     if ((status = makelist(f)) != TRUE)
  283.         return(status);
  284.     return(wpopup(blistp));
  285. }
  286.  
  287. /*
  288.  * This routine rebuilds the
  289.  * text in the special secret buffer
  290.  * that holds the buffer list. It is called
  291.  * by the list buffers command. Return TRUE
  292.  * if everything works. Return FALSE if there
  293.  * is an error (if there is no memory). Iflag
  294.  * indecates weather to list hidden buffers.
  295.  */
  296. PASCAL NEAR makelist(iflag)
  297.  
  298. int iflag;    /* list hidden buffer flag */
  299. {
  300.     register char    *cp1;
  301.     register char    *cp2;
  302.     register int    c;
  303.     register BUFFER *bp;
  304.     register LINE    *lp;
  305.     register int    s;
  306.     register int    i;
  307.     long nbytes;        /* # of bytes in current buffer */
  308.     char b[7+1];
  309.     char line[128];
  310.  
  311.     blistp->b_flag &= ~BFCHG;        /* Don't complain!    */
  312.     if ((s=bclear(blistp)) != TRUE)     /* Blow old text away    */
  313.         return(s);
  314.     strcpy(blistp->b_fname, "");
  315.     if (addline(blistp, TEXT30) == FALSE
  316. /*            "ACT   Modes      Size Buffer       File" */
  317.     ||  addline(blistp, "--- --------- ------- --------------- ----") == FALSE)
  318.         return(FALSE);
  319.     bp = bheadp;                /* For all buffers    */
  320.  
  321.     /* build line to report global mode settings */
  322.     cp1 = &line[0];
  323.     *cp1++ = ' ';
  324.     *cp1++ = ' ';
  325.     *cp1++ = ' ';
  326.     *cp1++ = ' ';
  327.  
  328.     /* output the mode codes */
  329.     for (i = 0; i < NUMMODES; i++)
  330.         if (gmode & (1 << i))
  331.             *cp1++ = modecode[i];
  332.         else
  333.             *cp1++ = '.';
  334.     strcpy(cp1, TEXT31);
  335. /*            "          Global Modes" */
  336.     if (addline(blistp, line) == FALSE)
  337.         return(FALSE);
  338.  
  339.     /* output the list of buffers */
  340.     while (bp != NULL) {
  341.         /* skip invisable buffers if iflag is false */
  342.         if (((bp->b_flag&BFINVS) != 0) && (iflag != TRUE)) {
  343.             bp = bp->b_bufp;
  344.             continue;
  345.         }
  346.         cp1 = &line[0];         /* Start at left edge    */
  347.  
  348.         /* output status of ACTIVE flag (has the file been read in? */
  349.         if (bp->b_active == TRUE)    /* "@" if activated       */
  350.             *cp1++ = '@';
  351.         else
  352.             *cp1++ = ' ';
  353.  
  354.         /* output status of changed flag */
  355.         if ((bp->b_flag&BFCHG) != 0)    /* "*" if changed    */
  356.             *cp1++ = '*';
  357.         else
  358.             *cp1++ = ' ';
  359.  
  360.         /* report if the file is truncated */
  361.         if ((bp->b_flag&BFTRUNC) != 0)
  362.             *cp1++ = '#';
  363.         else
  364.             *cp1++ = ' ';
  365.  
  366.         *cp1++ = ' ';    /* space */
  367.  
  368.         /* output the mode codes */
  369.         for (i = 0; i < NUMMODES; i++) {
  370.             if (bp->b_mode & (1 << i))
  371.                 *cp1++ = modecode[i];
  372.             else
  373.                 *cp1++ = '.';
  374.         }
  375.         *cp1++ = ' ';            /* Gap.         */
  376.         nbytes = 0L;            /* Count bytes in buf.    */
  377.         lp = lforw(bp->b_linep);
  378.         while (lp != bp->b_linep) {
  379.             nbytes += (long)llength(lp)+1L;
  380.             lp = lforw(lp);
  381.         }
  382.         long_asc(b, 7, nbytes);         /* 6 digit buffer size. */
  383.         cp2 = &b[0];
  384.         while (*cp2)
  385.             *cp1++ = *cp2++;
  386.         *cp1++ = ' ';            /* Gap.         */
  387.         cp2 = &bp->b_bname[0];        /* Buffer name        */
  388.         while (*cp2)
  389.             *cp1++ = *cp2++;
  390.         *cp1++ = ' ';            /* Gap.         */
  391.         cp2 = &bp->b_fname[0];        /* File name        */
  392.         if (*cp2 != 0) {
  393.             while (cp1 < &line[38])
  394.                 *cp1++ = ' ';
  395.             while (*cp2)
  396.                 *cp1++ = *cp2++;
  397.         }
  398.         *cp1 = 0;          /* Add to the buffer.   */
  399.         if (addline(blistp, line) == FALSE)
  400.             return(FALSE);
  401.         bp = bp->b_bufp;
  402.     }
  403.     return(TRUE);                   /* All done           */
  404. }
  405.  
  406. /* Translate a long to ascii form. Don't trust various systems
  407.    ltoa() routines.. they aren't consistant                */
  408.  
  409. PASCAL NEAR long_asc(buf, width, num)
  410.  
  411. char   buf[];
  412. int    width;
  413. long   num;
  414.  
  415. {
  416.     buf[width] = 0;             /* End of string.    */
  417.     while (num >= 10) {            /* Conditional digits.    */
  418.         buf[--width] = (int)(num%10L) + '0';
  419.         num /= 10L;
  420.     }
  421.     buf[--width] = (int)num + '0';        /* Always 1 digit.    */
  422.     while (width != 0)            /* Pad with blanks.    */
  423.         buf[--width] = ' ';
  424. }
  425.  
  426. /*
  427.  * Look through the list of
  428.  * buffers. Return TRUE if there
  429.  * are any changed buffers. Buffers
  430.  * that hold magic internal stuff are
  431.  * not considered; who cares if the
  432.  * list of buffer names is hacked.
  433.  * Return FALSE if no buffers
  434.  * have been changed.
  435.  */
  436. PASCAL NEAR anycb()
  437. {
  438.     register BUFFER *bp;
  439.  
  440.     bp = bheadp;
  441.     while (bp != NULL) {
  442.         if ((bp->b_flag&BFINVS)==0 && (bp->b_flag&BFCHG)!=0)
  443.             return(TRUE);
  444.         bp = bp->b_bufp;
  445.     }
  446.     return(FALSE);
  447. }
  448.  
  449. /*
  450.  * Find a buffer, by name. Return a pointer
  451.  * to the BUFFER structure associated with it.
  452.  * If the buffer is not found
  453.  * and the "cflag" is TRUE, create it. The "bflag" is
  454.  * the settings for the flags in in buffer.
  455.  */
  456. BUFFER *PASCAL NEAR bfind(bname, cflag, bflag)
  457.  
  458. register char    *bname; /* name of buffer to find */
  459. int cflag;        /* create it if not found? */
  460. int bflag;        /* bit settings for a new buffer */
  461.  
  462. {
  463.     register BUFFER *bp;
  464.     register BUFFER *sb;    /* buffer to insert after */
  465.     register LINE    *lp;
  466.     int cmark;        /* current mark */
  467.  
  468.     bp = bheadp;
  469.     while (bp != NULL) {
  470.         if (strcmp(bname, bp->b_bname) == 0)
  471.             return(bp);
  472.         bp = bp->b_bufp;
  473.     }
  474.     if (cflag != FALSE) {
  475.         if ((bp=(BUFFER *)malloc(sizeof(BUFFER))) == NULL)
  476.             return(NULL);
  477.         if ((lp=lalloc(0)) == NULL) {
  478.             free((char *) bp);
  479.             return(NULL);
  480.         }
  481.         /* find the place in the list to insert this buffer */
  482.         if (bheadp == NULL || strcmp(bheadp->b_bname, bname) > 0) {
  483.             /* insert at the beginning */
  484.             bp->b_bufp = bheadp;
  485.             bheadp = bp;
  486.         } else {
  487.             sb = bheadp;
  488.             while (sb->b_bufp != NULL) {
  489.                 if (strcmp(sb->b_bufp->b_bname, bname) > 0)
  490.                     break;
  491.                 sb = sb->b_bufp;
  492.             }
  493.  
  494.             /* and insert it */
  495.             bp->b_bufp = sb->b_bufp;
  496.             sb->b_bufp = bp;
  497.         }
  498.  
  499.         /* and set up the other buffer fields */
  500.         bp->b_topline = NULL;
  501.         bp->b_botline = NULL;
  502.         bp->b_active = TRUE;
  503.         bp->b_dotp  = lp;
  504.         bp->b_doto  = 0;
  505.         for (cmark = 0; cmark < NMARKS; cmark++) {
  506.             bp->b_markp[cmark] = NULL;
  507.             bp->b_marko[cmark] = 0;
  508.         }
  509.         bp->b_fcol = 0;
  510.         bp->b_flag  = bflag;
  511.         bp->b_mode  = gmode;
  512.         bp->b_nwnd  = 0;
  513.         bp->b_linep = lp;
  514.         strcpy(bp->b_fname, "");
  515.         strcpy(bp->b_bname, bname);
  516. #if    CRYPT
  517.         bp->b_key[0] = 0;
  518. #endif
  519. #if    FTYPE
  520.         bp->b_type = B_DEFTYPE;
  521. #endif
  522.                 lp->l_fp = lp;
  523.                 lp->l_bp = lp;
  524.         }
  525.         return(bp);
  526. }
  527.  
  528. /*
  529.  * This routine blows away all of the text
  530.  * in a buffer. If the buffer is marked as changed
  531.  * then we ask if it is ok to blow it away; this is
  532.  * to save the user the grief of losing text. The
  533.  * window chain is nearly always wrong if this gets
  534.  * called; the caller must arrange for the updates
  535.  * that are required. Return TRUE if everything
  536.  * looks good.
  537.  */
  538. PASCAL NEAR bclear(bp)
  539. register BUFFER *bp;
  540. {
  541.     register LINE    *lp;
  542.     register int    s;
  543.     int cmark;        /* current mark */
  544.  
  545.     if ((bp->b_flag&BFINVS) == 0        /* Not scratch buffer.    */
  546.     && (bp->b_flag&BFCHG) != 0        /* Something changed    */
  547.     && (s=mlyesno(TEXT32)) != TRUE)
  548. /*              "Discard changes" */
  549.         return(s);
  550.     bp->b_flag  &= ~BFCHG;            /* Not changed        */
  551.     while ((lp=lforw(bp->b_linep)) != bp->b_linep)
  552.         lfree(lp);
  553.     bp->b_dotp  = bp->b_linep;        /* Fix "."        */
  554.     bp->b_doto  = 0;
  555.     for (cmark = 0; cmark < NMARKS; cmark++) {
  556.         bp->b_markp[cmark] = NULL;  /* Invalidate "mark"    */
  557.         bp->b_marko[cmark] = 0;
  558.     }
  559.     bp->b_fcol = 0;
  560.     return(TRUE);
  561. }
  562.  
  563. PASCAL NEAR unmark(f, n)    /* unmark the current buffers change flag */
  564.  
  565. int f, n;    /* unused command arguments */
  566.  
  567. {
  568.     register WINDOW *wp;
  569.  
  570.     /* unmark the buffer */
  571.     curbp->b_flag &= ~BFCHG;
  572.  
  573.     /* unmark all windows as well */
  574.     upmode();
  575.  
  576.     return(TRUE);
  577. }
  578.